""" 
Author hza
Date 2022/8/11
"""

# ############################## 1、多进程实现网段存活检测
from pythonping import ping
from multiprocessing import Lock, Process


def Ping(i):
    with Lock():
        result = ping(f"10.0.12.{i}", verbose=True, count=1, timeout=1)
        if "Reply" in str(result):
            print(f"10.0.12.{i} is reachable")
        else:
            print(f"10.0.12.{i} is unreachable")


if __name__ == '__main__':
    p_list = []
    for i in range(1, 255):
        p = Process(target=Ping, args=(i,))
        p_list.append(p)
        p.start()
    for p in p_list:
        p.join()
    print("ending......")

# ############################### 2、任意网站  三创官网爬取图片

import requests
import re
import os

url = "https://www.sanchuangedu.cn/"
r = requests.get(url)
r.encoding = "utf8"
rtext = r.text
img_list = re.findall(r"(?<=<img src=).*[.jpg|.png]", rtext)

for addr in img_list:
    src = 'https://www.sanchuangedu.cn/' + addr
    imgdata = requests.get(src).content
    imgpath = 'D:\\python\\代码\\img\\'+addr
    with open(imgpath, 'wb') as fp:
        fp.write(imgdata)
        print(f'{addr} 下载成功！')
